// 同事类
class Colleague {
    mediator: Mediator
    constructor(mediator: Mediator) {
        this.mediator = mediator
    }
    send(message: string) {
        this.mediator.send(message, this)
    }
    receive(message: string) {
        console.log(message)
    }
}

// 具体同事类
class ConcreteColleague1 extends Colleague {
    constructor(mediator: Mediator) {
        super(mediator)
    }
    send(message: string) {
        super.send(message)
    }
    receive(message: string) {
        super.receive(message)
    }
}

class ConcreteColleague2 extends Colleague {
    constructor(mediator: Mediator) {
        super(mediator)
    }
    send(message: string) {
        super.send(message)
    }
    receive(message: string) {
        super.receive(message)
    }
}

// 中介者
class Mediator {
    colleague1: ConcreteColleague1
    colleague2: ConcreteColleague2
    constructor() {
        this.colleague1 = new ConcreteColleague1(this)
        this.colleague2 = new ConcreteColleague2(this)
    }
    send(message: string, colleague: Colleague) {
        if (colleague === this.colleague1) {
            this.colleague2.receive(message)
        } else {
            this.colleague1.receive(message)
        }
    }
}

// 使用
const mediator = new Mediator()
mediator.colleague1.send('hello')
mediator.colleague2.send('world')